<?php
class push extends core {
    public function __construct() {
        parent::__construct();
    }

    //index
    public function index() {
        $where = '';
        $id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : '';
        $keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
        if ($keyword) {
            $where .= "AND (p.title LIKE '%{$keyword}%' OR p.content LIKE '%{$keyword}%')";
        }
        if ($id) {
            $where .= "AND p.id='{$id}'";
        }
        //分页
        $this->setpages(array('id'=>$id, 'keyword'=>$keyword));
        $sql = "SELECT p.* FROM jf_push p 
            WHERE 1 {$where} ORDER BY p.id DESC";
        $rs = $this->ezr->get_results($sql);
        $nav = $this->ezr->get_navigation();
        $this->smarty->assign('rs',$rs);
        $this->smarty->assign('page',$nav);
        $this->display();
    }

    //add
    public function add() {
        if (IS_POST) {
            $this->save();
        } else {
            $this->display();
        }
    }   

    //edit
    public function edit() {
        if (IS_POST) { //修改
            $this->save();
        } else { //显示
            $id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
            $sql = "SELECT * FROM jf_push WHERE id='{$id}'";
            $row = $this->db->get_row($sql);
            $this->smarty->assign('row', $row);
            $this->display();
        }
    }
    
    //写入到数据库
    public function save() {
        $id = (isset($_POST['id']) && trim($_POST['id'])) ? intval($_POST['id']) : 0;
        $title = (isset($_POST['title']) && trim($_POST['title'])) ? trim($_POST['title']) : '';
        $content = (isset($_POST['content']) && trim($_POST['content'])) ? trim($_POST['content']) : '';
        $type_id = (isset($_POST['type_id']) && trim($_POST['type_id'])) ? intval($_POST['type_id']) : 0;
        $url = (isset($_POST['url']) && trim($_POST['url'])) ? trim($_POST['url']) : '';
        $extra_id =  (isset($_POST['extra_id']) && trim($_POST['extra_id'])) ? intval($_POST['extra_id']) : 0;
        $member_type =  (isset($_POST['member_type']) && trim($_POST['member_type'])) ? intval($_POST['member_type']) : 0;
        $members =  (isset($_POST['members']) && intval($_POST['members'])) ? $_POST['members'] : 0;
        $board_id = (isset($_POST['board_id']) && trim($_POST['board_id'])) ? intval($_POST['board_id']) : 0;
        if($id>0)   //edit
        {

        }else{    //add
            $sql = "INSERT INTO jf_push(title, content, member_type, add_time, send_time, push_type, push_type_id, push_url)
                  VALUES ('{$title}', '{$content}','{$member_type}','".time()."','".time()."', '{$type_id}', '{$extra_id}', '{$url}')";
            $this->db->query($sql);
            $id = $this->db->insert_id;
        }
        $sql = "INSERT INTO jf_push_member(push_id,member_id) VALUES ";
        if($members!=0){
            foreach ($members as $member) {
                $sql .= "('{$id}','{$member}'),";
            }
            $sql = substr($sql, 0, strlen($sql) - 1);
            $this->db->query($sql);
        }
        if($type_id!=1)
        {
            $this->send_cast($title,$content,$type_id,$extra_id,$members,$board_id);
        }else
        {
            $this->send_cast($title,$content,$type_id,$url,$members,$board_id);
        }
        header("Location:?app=push&act=index");
    }
    //delete
    public function delete() {
        $id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
        $sql = "DELETE FROM jf_push WHERE id='{$id}'";
        $this->db->query($sql);
        //exit;
        header("Location:?app=push");
    }

    //发送广播
    public function send_cast($title,$content,$type_id,$extra,$members,$board_id){
        switch($type_id)
        {
            case '1':
                $activity ="com.lichi.imei.activity.ADWebViewActivity" ;
                break;
            case '2':
                $activity = "com.lichi.imei.activity.ArticleDetailActivity" ;
                break;
            case '3':
                $activity = "com.lichi.imei.activity.TopicDetailActivity" ;
                break;
            case '4':
                $activity = "com.lichi.imei.activity.ComplainDetailActivity";
                break;
            default:
                $activity = '';
                break;
        }
        if($members==0)
        {
            $this->send_all_brocast($title,$title,$content,$activity,$extra,$type_id,$board_id);
        } else{
            $sql ="SELECT udid from jf_member WHERE id in(";
            foreach($members as $member)
            {
                $sql.="'{$member}',";
            }
            $sql = substr($sql,0,strlen($sql)-1).")";
            $members_udid = $this->db->get_results($sql);
            $android_device_tokens='';
            $ios_device_tokens='';
            foreach($members_udid as $udid)
            {
                if(strlen($udid->udid)<64)
                {
                    $android_device_tokens.=$udid->udid.",";
                }else{
                    $ios_device_tokens.=$udid->udid.",";
                }
            }
            $android_device_tokens = substr($android_device_tokens,0,strlen($android_device_tokens)-1);
            $ios_device_tokens = substr($ios_device_tokens,0,strlen($ios_device_tokens)-1);
            //echo $android_device_tokens;
            //echo " ";
            //echo $ios_device_tokens;
            //exit;
            $this->send_all_listcast($title, $title, $content, $activity, $extra, $android_device_tokens,$ios_device_tokens, $type_id,$board_id);
        }
    }
}
